import time
import requests
import execjs
import random
a = input('输入翻译内容')

r = str(int(time.time()*1000))
i = r + str(int(10 * random.random()))
node = execjs.get()
ctx = node.compile(open('/Users/lidong/Desktop/zhouyu/BILIBILI/笔记/爬虫/youdao.js', encoding='utf-8').read())
funcName = "getpwd('{0}','{1}',{2})".format(a,  r, i)
sign = ctx.eval(funcName)
print(sign)


url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
data = {
    'i': a,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': i,
    'sign': sign,
    'lts': r,
    'bv': '8189d11f9c15277541fd040e501a0cde',
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_CLICKBUTTION'
}

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Cache-Control':'no-cache',
    'Connection': 'keep-alive',
    'Content-Length': '241',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'OUTFOX_SEARCH_USER_ID=1865090727@10.110.96.154; OUTFOX_SEARCH_USER_ID_NCOO=897878350.537475; fanyi-ad-id=305838; fanyi-ad-closed=1; ___rl__test__cookies=1652840735922',
    'Host': 'fanyi.youdao.com',
    'Origin': 'https://fanyi.youdao.com',
    'Pragma': 'no-cache',
    'Referer': 'https://fanyi.youdao.com/',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': "macOS",
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
    }

req = requests.post(url,headers=headers, data=data).json()
print(req)

